package leetcode.code0898;

import java.util.HashSet;
import java.util.Set;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int subarrayBitwiseORs2(int[] arr) {
		int ans = 0;
		Set<Integer> all = new HashSet<>();
		Set<Integer> end = new HashSet<>();
		for (int i = 0; i < arr.length; i++) {
			int num = arr[i];
			if (all.add(num)) {
				ans += 1;
			}
			Set<Integer> tmp = new HashSet<>();
			for (int n : end) {
				n |= num;
				if (all.add(n)) {
					ans += 1;
				}
				tmp.add(n);
			}
			tmp.add(num);
			end = tmp;
		}
		return ans;
	}

	public int subarrayBitwiseORs(int[] arr) {
		Set<Integer> all = new HashSet<>();
		Set<Integer> end = new HashSet<>();
		for (int i = 0; i < arr.length; i++) {
			int num = arr[i];
			end.add(num);
			Set<Integer> tmp = new HashSet<>();
			for (int n : end) {
				n |= num;
				all.add(n);
				tmp.add(n);
			}
			end = tmp;
		}
		return all.size();
	}
	
	public int subarrayBitwiseORs3(int[] arr) {
		Set<Integer> all = new HashSet<>();
		Set<Integer> end = new HashSet<>();
		for (int i = 0; i < arr.length; i++) {
			int num = arr[i];
			Set<Integer> tmp = new HashSet<>();
			for (int n : end) {
				tmp.add(n | num);
			}
			tmp.add(num);
			end = tmp;
			all.addAll(end);
		}
		return all.size();
	}

	@Override
	public void debug4() {
		H.compare(6, this.subarrayBitwiseORs(H.str2array3("[1,2,4]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		
	}

}
